#include "settingwidget.h"
#include "ui_settingwidget.h"
#include <QFileDialog>
#include <QDebug>

SettingWidget::SettingWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SettingWidget)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_TranslucentBackground, true);
    basePath = qApp->applicationDirPath();
    getConfig();
}

SettingWidget::~SettingWidget()
{
    delete ui;
}

void SettingWidget::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    painter.fillRect(this->rect(), QColor(0, 0, 0, 0));  //QColor最后一个参数80代表背景的透明度
}


void SettingWidget::on_getMap_clicked()
{
    QFileDialog dir;
    dir.setWindowFlags(Qt::WindowStaysOnTopHint);
    this->hide();
    QString fileName = dir.getOpenFileName(NULL, "请打开地图文件", basePath);
//    QString fileName = QFileDialog::getOpenFileName(NULL, "请打开地图文件", basePath);
    if(fileName.isNull())
        return;
    fileName = fileTrans(fileName);
    ui->pathMap->setText(fileName);
    setConfig("MapPath", fileName);
    this->show();
    emit updateMap();
}

void SettingWidget::on_getStation_clicked()
{
    QFileDialog dir;
    dir.setWindowFlags(Qt::WindowStaysOnTopHint);
    this->hide();
    QString fileName = dir.getOpenFileName(NULL, "请打开站点文件", basePath);
    if(fileName.isNull())
        return;

    ui->pathStation->setText(fileName);
    setConfig("StationPath", fileName);
    this->show();
    emit updateStation();
}

void SettingWidget::on_ok_clicked()
{
    this->close();
}

QString SettingWidget::fileTrans(QString fileName)
{
    qDebug()<<"readPathPoint"<<fileName;
    if(fileName.isEmpty())
        return fileName;
    if(fileName.right(3) == ".bs")
    {
        qDebug()<<"don't need trans";
        return fileName;
    }

    QFile f(fileName);
    f.open(QFile::ReadOnly);

    QVector<QPointF> list_path;
    QByteArray qba;
    if(!list_path.isEmpty())
    {
        list_path.clear();
        list_path.squeeze();
    }

    int i=0;
    while(!(qba = f.readLine()).isEmpty())
    {
        QString str = QString(qba);
//        qDebug()<<"[readPoint]"<<str;
        QStringList l = str.split(" ", QString::SkipEmptyParts);
        if(l.count() < 2)
        {
            qDebug()<<"[readPoint]:File fomat error.";
            return fileName;
        }
        bool ok;
        QPointF p;
        p.setX(l.at(0).toDouble(&ok));
        if(!ok)
        {
            qDebug()<<"[readPoint]:File fomat error.";
            return fileName;
        }
        p.setY(l.at(1).toDouble(&ok));
        if(!ok)
        {
            qDebug()<<"[readPoint]:File fomat error.";
            return fileName;
        }
        if(i%10 == 0)
            list_path.push_back(p);
        i++;
    }
    f.close();

    BSpline b_spline;
    QVector<QPointF> pathPoints = b_spline.generateCurve(list_path, 3, 0.1);
    fileName = fileName+".bs";
    QFile nf(fileName);
    nf.open(QFile::WriteOnly);
    QPointF p;
    foreach(p, pathPoints)
    {
        nf.write((QString("%1 %2\n").arg(QString::number(p.x(),'f')).arg(QString::number(p.y(), 'f'))).toLocal8Bit());
    }
    nf.close();
    return fileName;
}

void SettingWidget::setConfig(QString key, QString val)
{
    QSettings settings(basePath+"/xingyun.conf", QSettings::IniFormat);
    settings.setValue(key,val);
    settings.sync();
}

void SettingWidget::getConfig()
{
    QSettings settings(basePath+"/xingyun.conf", QSettings::IniFormat);
    ui->pathMap->setText(settings.value("MapPath", QString()).toString());
    ui->pathStation->setText(settings.value("StationPath",QString()).toString());
}
